import 'package:flutter/material.dart';
import 'package:flutter_utils/flutter_utils.dart';
import 'package:from_css_color/from_css_color.dart';

enum ButtonType {
  primary,
  transparent,
  border,
}

class PrimaryButton extends StatefulWidget {
  final double? width;
  final double? height;
  final VoidCallback? onPressed;
  final Widget? child;
  final ButtonType? buttonType;
  final double? radius;

  const PrimaryButton(
      {super.key,
      this.width,
      this.height,
      this.child,
      this.onPressed,
      this.radius})
      : buttonType = ButtonType.primary;
  const PrimaryButton.warp(
      {super.key,
      this.width,
      this.height,
      this.child,
      this.onPressed,
      this.radius})
      : buttonType = ButtonType.transparent;
  const PrimaryButton.border(
      {super.key,
      this.width,
      this.height,
      this.child,
      this.onPressed,
      this.radius})
      : buttonType = ButtonType.border;

  @override
  State<PrimaryButton> createState() => _PrimaryButtonState();
}

class _PrimaryButtonState extends State<PrimaryButton> {
  /// 事件是否可执行
  bool enable = true;
  void throttleIncrease() async {
    if (enable) {
      enable = false;
      widget.onPressed?.call();
      await Future.delayed(const Duration(seconds: 1));
      enable = true;
    }
  }

  @override
  Widget build(BuildContext context) {
    BoxDecoration? decoration;
    switch (widget.buttonType) {
      case ButtonType.primary:
        decoration = BoxDecoration(
          borderRadius: BorderRadius.circular(widget.radius ?? 10.ratio),
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [fromCssColor('#4ABCEB'), fromCssColor('#3166E1')],
          ),
        );
        break;
      case ButtonType.border:
        decoration = BoxDecoration(
          borderRadius: BorderRadius.circular(widget.radius ?? 10.ratio),
          border: Border.all(color: fromCssColor('#4ABCEB'), width: 1.ratio),
        );
        break;
      default:
        decoration = null;
    }

    return InkWell(
      onTap: throttleIncrease,
      child: Container(
        width: widget.width,
        height: widget.height,
        alignment: Alignment.center,
        decoration: decoration,
        child: widget.child,
      ),
    );
  }
}
